// pages/post-detail/post-detail-buy/post-detail-buy.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    code: '',
    phone: '',
    product_name: '',
    sum: 0,
    all_price: '',
    actual_price: '',
    product_price: '',
    order_number: '',
    consume_time: '',
    payment_time: '',
    return_money: 0,
    order_company: '',
    istui: 1,
    isis:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    console.log(111)
    this.setData({
      code: options.id
    })
    var that = this
    this.onChecked();
    this.phone();


  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    console.log(111)
  },
  onChecked() {
    console.log(this.data.code)
    let tableName = 'Order'

    let Query = new wx.BaaS.Query()
    Query.compare('transaction_no', '=', this.data.code)
    Query.compare('phone', '=', app.globalData.phone)
    let Product = new wx.BaaS.TableObject(tableName)
    Product.setQuery(Query).limit(1000).offset(0).find().then(res => {
      // success
      // console.log(res.data.objects[0].service_name)
      if (res.data.objects[0].product_code != '' && res.data.objects[0].service_name == undefined) {
        console.log(res)
        this.setData({
          index: 1,
          item: res.data.objects, //用户
        })
        app.globalData.Order = this.data.item
        this.Product()
        console.log(this.data.item[0])
      } else if (res.data.objects[0].service_name != '') {
        var item = res.data.objects
        console.log(item)
        app.globalData.Order = item
        console.log(app.globalData.service_items_pic)
        this.setData({
          index: 0,
          product_name:item[0].name,
          phone: app.globalData.phone,
          service_name: item[0].service_name,
          service_time: item[0].service_time,
          all_price: parseInt(item[0].all_price),
          actual_price: item[0].actual_price + '.00',
          transaction_no: item[0].transaction_no,
          payment_time: item[0].payment_time,
          fukuan: item[0].fukuan,
          shouhuo: item[0].shouhuo,
          fahuo: item[0].fahuo,
          tuihuo: item[0].tuihuo,
          istuihuo: item[0].istuihuo,
          product_pic: app.globalData.service_items_pic,
          address: item[0].address,
          id: item[0].id,
          product_price:app.globalData.service_items_cost
        })
      }

    }, err => {
      // err
    })
  },
  Product() {
    var that = this
    for (var i = 0; i < app.globalData.product_sum; i++) {
      if (that.data.item[0].product_code == app.globalData.product[i].product_code) {
        console.log(app.globalData.product[i])
        that.setData({
          phone: that.data.item[0].phone,
          product_name: that.data.item[0].name,
          sum: that.data.item[0].sum,
          all_price: parseInt(app.globalData.product[i].cost) * that.data.item[0].sum,
          actual_price: that.data.item[0].actual_price + '.00',
          product_price: that.data.item[0].all_price,
          order_number: that.data.item[0].order_number,
          order_company: that.data.item[0].order_company,
          transaction_no: that.data.item[0].transaction_no,
          payment_time: that.data.item[0].payment_time,
          return_money: that.data.item[0].return_money,
          fukuan: that.data.item[0].fukuan,
          shouhuo: that.data.item[0].shouhuo,
          fahuo: that.data.item[0].fahuo,
          tuihuo: that.data.item[0].tuihuo,
          istuihuo: that.data.item[0].istuihuo,
          product_pic: app.globalData.product[i].product_swiper_pic1,
          address: that.data.item[0].address,
          id: that.data.item[0].id
        })
      }
      if (this.data.tuihuo == 1 && this.data.fukuan == 1 && this.data.istuihuo == 0 || this.data.fukuan == 1 && this.data.tuihuo == 0 && this.data.fahuo == 1 & this.data.shouhuo == 1) {
        this.setData({
          istui: 0
        })
      }
      if (this.data.tuihuo == 1 && this.data.fukuan == 1 && this.data.istuihuo == 0) {
        this.setData({
          istui: 2
        })
      }
    }
  },
  goAddress: function (e) {
    if (this.data.tuihuo == 1 || this.data.shouhuo == 1 && this.data.fahuo == 1 || this.data.fukuan == 0 || this.data.shouhuo == 1) {
      wx.showToast({
        title: '无法修改哦',
        icon: 'none'
      })
    } else if (this.data.fahuo == 0 && this.data.fukuan == 1 && this.data.tuihuo == 0) {
      this.setData({
        isis:true
      })
      wx.navigateTo({
        url: '/pages/addresDetail/addresDetail',
      })
    }

  },
  tuihuozhuantai: function (e) {
    wx.navigateTo({
      url: '/pages/tuikuan/tuikuan',
    })
  },
  /**
   * 电话遮盖
   */
  phone() {
    var phone = []
    var phone = this.data.phone.split("")
    var iphone = ''
    for (var i = 0; i < 11; i++) {
      if (i > 6) {
        phone[i] = '*'
        console.log(i)
      }
      iphone.concat(phone[i])
    }
    

    this.setData({
      phone: iphone
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (this.data.address != app.globalData.address && this.data.isis == true) {
      this.setData({
        address: app.globalData.address
      })
      console.log(this.data.id)
      let tableName = 'Order'
      let recordID = this.data.id // 数据行 id

      let Product = new wx.BaaS.TableObject(tableName)
      let product = Product.getWithoutData(recordID)

      product.set('address', this.data.address)
      product.update().then(res => {
        // success
      }, err => {
        // err
      })
    }

  },
  tuihuo: function (e) {
    wx.navigateTo({
      url: '/pages/after-sale/after-sale?id=' + this.data.transaction_no,
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  onFuZhiTap: function (e) {
    wx.setClipboardData({
      data: this.data.order_number,
    })
  }
})